- [Instructor] In electronics projects,
it's common to use a microcontroller board
like an Arduino or a Raspberry Pi
to control other devices, turning them on or off.
But, using an Arduino to control something else,
like an electric motor, is a bit more complicated than
simply connecting it to the Arduino's digital output pins.
For one, the motor probably requires a different voltage
than the Arduino's output.
My little DC motor is designed to operate at six volts,
but my Arduino only outputs a five-volt signal.
Another problem is that the amount of output current
that an Arduino board can produce
is probably a lot less than I need to power my motor.
What I need here is for the motor to get its power
from a dedicated six-volt power supply,
but I need the Arduino to control when it's on or off.
To do that, I can use the transistor
as an electrically controlled switch.
To interface between the two components
and enable the Arduino to turn the power on or off
to the motor.
Unlike a mechanical switch, which contains parts that
physically move to open or close a circuit,
a transistor switch is turned on or off electronically
by controlling the voltage and current
at the transistor's base terminal to put it
in either the saturation or the cut-off mode.
When the transistor is in the saturation mode,
it will act like a closed switch to turn on
whatever device is connected to its output.
And when the transistor is in the cut-off mode,
it will act like an open switch,
which turns off the connected device.
To use an NPN transistor as a switch,
I'll connect the source of my control signal
to the base of the transistor.
And, to limit the amount of current
that the transistor draws
from the controlling device when it's turned on,
I'll include a resistor in series
between the base and control signal source.
Next, I'll tie the emitter terminal
to a low fixed voltage, which is almost always ground,
and I'll connect the collector pin
to the device I want to control power to,
which will be connected to its power source
on the other side, as shown here.
Since this circuit will be switching the motor on and off,
and motors tend to act as an inductive load,
I'll also include a flyback diode here
to protect my circuit from potentially damaging
flyback voltage spikes.
To turn this motor on, all I need to do is
raise the control voltage enough,
so that the base emitter junction turns on,
which is usually around .7 volts
and then provide enough current into the base
to saturate the transistor.
When it's saturated, the transistor acts similar to
a short circuit, between the collector and emitter,
which is like a closed switch.
The motor's turned on,
and it draws current from the six-volt power supply.
To turn the motor off,
I simply lower the control signal down to ground,
which means the voltage between the base and emitter
will be less than the threshold to turn on the transistor.
The transistor will be in the cut-off mode,
where it acts like an open switch
to prevent current from flowing through the motor.
Most microcontroller devices,
like an Arduino or Raspberry Pi,
output digital signals that switch
between zero volts to represent a low value
and 3.3 or five volts to represent a digital high value.
And those voltages work great for toggling
a bipolar junction transistor switch on or off.
However, the output pins in a microcontroller board
are usually only designed to output
a small amount of current.
If the transistor tries to draw
more current than it can handle,
that might damage the controller.
When the transistor is turned on,
the junction between the base and emitter
acts like a forward-biased diode,
which allows current to flow freely into the base.
And that's why it's important to include
a limiting resistor between
the source of the control signal and the base terminal.
Without that limiting resistor there,
the transistor might try to draw more current
than the attached controlling device can provide.
So, this resistor serves a similar purpose
to the current-limiting resistor connected in series
with the light-emitting diode.
As a rule of thumb,
for most microcontroller-based hobby projects,
one kilohm is a good value to use
for the current-limiting resistor.
If I connect this circuit to the digital output line
of an Arduino microcontroller,
which outputs five volts to represent digital high,
.7 volts of that will dropped
between the transistor's base and emitter terminals,
which leaves 4.3 volts to be dropped
across the one kilohm resistor.
According to Ohm's Law,
4.3 volts dropped across a one-kilohm resistor
will only draw 4.3 milliamps from the Arduino,
which is well within the current output capabilities
of my Arduino microcontroller board.
The other current that I need to consider
is the current between the collector and the emitter.
When the transistor is switched on,
all of the current going through the motor
will flow into the collector terminal
and pass through the transistor.
So, I need to make sure that I select a transistor
that can dissipate enough power
to handle that much collector current.
The amount of current through an electric motor
is based on the applied voltage across the motor
and its internal resistance.
For a simple example, I'll treat that motor as if
it's a 100-ohm resistor in series with an inductor.
When the transistor is turned on and saturated,
there will be a .2-volt drop
between the collector and emitter terminals,
which leaves 5.8 volts to be dropped across the motor
coming from its six-volt power source.
According to Ohm's Law, 5.8 volts across a 100-ohm resistor
allows 58 milliamps of current to flow into
the transistor's collector.
If I'm using a 2N3904 BJT,
which can handle a continuous collector current
of 200 milliamps, then this circuit should work just fine.
If you're using a transistor to switch
other types of load devices, you'll need to consider
that device's unique current characteristics
to make sure your transistor can handle it.
Now, to demonstrate this circuit,
I've written a simple Arduino program
that repeatedly toggles digital output pin zero
between its high state and its low state
about once every second.
I've included the program in the exercise files as
Transistor_Switch_Arduino_Demo.ino
if you want to use it as a reference.
I've connected that control signal
from pin zero of my Arduino
to the base of NPN transistor,
and I'm using my oscilloscope to display that signal
on channel one in yellow.
My bunched up power supply is providing six volts
through the breadboard power bus to power the motor,
which is connected to the transistor's collector terminal.
Looking at the oscilloscope, I can see that
when the Arduino output goes high,
the transistor switch turns on and the motor spins.
And when the Arduino's output is low,
the transistor switch is off, so the motor stays still.
This type is circuit is called a low-side switch
because the NPN transistor is on the low or ground side
of the device it's controlling.
If I wanted to switch the polarity of the control signal
so that my transistor switch turns on
when the control voltage is low and off when it's high,
I'll use a PNP transistor instead.
But, to use a PNP transistor,
I'll need to create a high-side switch.
As with the NPN low-side switch,
the control signal is connected to
the PNP transistor's base with a resistor in series.
I've connected the PNP's emitter terminal
to a constant voltage, but this time,
it's the high voltage from the power supply.
And the motor I'm controlling is connected on the low side
between the PNP's collector pin and ground.
The input voltages I use to control a PNP high-side switch
are the opposite of the control signals
for an NPN low-side switch.
To turn the switch on, I'll need to provide
a high-voltage control signal
if I'm using the NPN switch,
and a low-voltage signal for the PNP switch.
And to turn the switch off,
the NPN switch requires a low-input signal,
whereas the PNP switch needs a high input.
As long as my controlling device can produce
the necessary high and low control voltages,
a PNP switch will work just as well
as its NPN counterpart.
But, it may not always be possible
to produce those required voltages.
One potential challenge of using the high-side PNP switch
is that, for it to turn off,
the control voltage needs to be high enough
that the voltage between the base and emitter
is less than the 0.7-volt threshold.
If I was using an Arduino to control
the circuit shown here,
since the Arduino's high output signal is only five volts,
but the motor's power source is connected to six volts,
my Arduino can't produce a high enough voltage
to turn this switch off.
This switch will forever be stuck on.
However, if the supply voltage for the motor was lower,
let's say just five volts,
then the PNP switch would work just fine.
This relationship between the control and supply voltages
is important to consider when choosing whether to use
an NPN or PNP transistor switch.
In general, I tend to use NPN low-side switches more often
than their PNP high-side counterpart
because I know I can always control it
with an Arduino or a Raspberry Pi,
regardless of the supply voltage
that's connected to the load.